package org.yuchen.controller;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ScopeServlet extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/*
		 * 通过request.getParameter()获取参数值的时候，页面上的元素一定要指定name属性，
		 * 否则获取不到value. 例如：<input type="text" name="username">
		 */
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String gender = req.getParameter("gender");
		
		req.setAttribute("username", username);
		req.getSession().setAttribute("password", password);
//		req.getServletContext().setAttribute("gender", gender);
//		resp.sendRedirect("./verifyScope.jsp");
		req.getRequestDispatcher("/verifyScope.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		String url = config.getInitParameter("url");
		System.out.println("NO2初始化： " + url);
	}
	
	public ScopeServlet() {
		System.out.println("容器通过反射创建ScopeServlet的实例");
		System.out.println("NO1实例化");
	}

	public void destroy() {
		super.destroy();
		System.out.println("容器会将Servlet实例从实例池中删除");
		System.out.println("NO4Servlet销毁");
	}
	
}
